<template>
  <div class="login-container">
    <div class="left">
    </div>
    <div class="right">
      <div class="header">
        <img src="/static/images/logo@2x.png" alt="">
        <p class="text">
          资源商管理发布平台
        </p>
      </div>
      <div class="container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="off" label-position="left">
          <el-form-item prop="username" style="background: rgba(247,247,247,1);border: 1px solid rgba(223, 223, 223, 1);box-shadow: 0px -1px 0px 0px rgba(255,255,255,0.72);border-radius: 2px;">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入管理账号"
              type="text"
              auto-complete="off"
              style="background: rgba(247,247,247,1);"/>
          </el-form-item>
          <el-form-item prop="password" style="background: rgba(247,247,247,1);border: 1px solid rgba(223, 223, 223, 1);box-shadow: 0px -1px 0px 0px rgba(255,255,255,0.72);border-radius: 2px;">
            <el-input
              :type="passwordType"
              v-model="loginForm.password"
              placeholder="请输入管理账号密码"
              name="password"
              auto-complete="off"
              @keyup.enter.native="handleLogin" />
          </el-form-item>
          <div style="margin-bottom: 22px;">
            <el-checkbox id="rememberPassword" v-model="checked">记住账密</el-checkbox>
          </div>
          <el-button
            :loading="loading"
            type="primary"
            style="width:100%;margin-bottom:30px;"
            @click.native.prevent="handleLogin">
            {{ $t('login.logIn') }}
          </el-button>
        </el-form>
        <div class="footer">
          <span>苏州哇喔信息科技有限公司 版权所有</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { isvalidUsername } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import SocialSign from './socialsignin'
import Cookies from 'js-cookie'
import cryptoJSON from 'crypto-json'

export default {
  name: 'Login',
  beforeCreate() {
    // const { params, query } = this.$route
    // const { path } = params
    // this.$router.replace({ path: '/' + path })
  },
  components: { LangSelect, SocialSign },
  data() {
    return {
      loginForm: {
        username: 'administrator',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请填写管理员账号' }],
        password: [{ required: true, trigger: 'blur', message: '请填写管理员密码' }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      checked: false,
      aseKey: 'jswei'
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        // this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  mounted() {
    this.$nextTick(_ => {
      this.getCookie()
    })
  },
  methods: {
    encode(secret) {
      var str = secret
      var r = ''
      for (var i = 0; i < str.length; i++) {
        var code = str.charCodeAt(i)
        r += code
        r += ','
      }
      return r
    },
    decode(secret) {
      var str = secret
      var arr = str.split(',')
      var r = ''
      for (var i = 0; i < arr.length; i++) {
        var code = parseInt(arr[i])
        r += String.fromCharCode(code)
      }
      return r
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (this.checked) {
            const data = {}
            data.checked = true
            data.password = this.loginForm.password
            data.username = this.loginForm.username
            this.setCookie(cryptoJSON.encrypt(data, this.aseKey))
          } else {
            Cookies.remove('$_login')
          }
          this.loading = true
          // this.$store.commit('SHOW_LOADING')
          this.$store.dispatch('LoginByUsername', this.loginForm).then(res => {
            this.loading = false
            // this.$store.commit('HIDE_LOADING')
            if (!res.status) {
              this.$message.error(res.msg)
              return
            }
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    setCookie(value) {
      value = JSON.stringify(value)
      Cookies.set('$_login', value, { expires: 7 })
    },
    getCookie() {
      const json = Cookies.getJSON('$_login')
      if (json) {
        const item = cryptoJSON.decrypt(json, this.aseKey)
        this.loginForm.username = item.username
        this.loginForm.password = item.password
        this.checked = item.checked
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .login-container {
    display: flex;
    height: 100%;
    .left{
      width: 30%;
      min-width: 500px;
      height: 100%;
      flex-direction: column;
      background: url('/static/images/bg@2x.png') no-repeat -100px -60px;
      padding: 0;
      margin: 0;
    }
    .right{
      width: 70%;
      height: 100%;
      flex-direction: column;
      .container{
        width: 450px;
        margin-top: 250px;
        margin-left: 180px;
      }
      .header{
        margin-top: 10px;
        margin-left: 10px;
        .text{
          margin: 5px 0;
          padding: 0;
        }
      }
    }
    .footer{
      margin-top: 10px;
    }
  }
</style>
